package com.wpq.game_company.controller;

import com.wpq.game_company.domain.Company;
import com.wpq.game_company.domain.Game;
import com.wpq.game_company.service.ICompanyService;
import com.wpq.game_company.service.impl.CompanyServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;

//创建一个bean放入容器
@Controller
//springmvc.xml文件启动注解
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    ICompanyService companyService;
    //或
    //CompanyServiceImpl companyService;


    //前端页面展示数据
    @RequestMapping("/allcompany")
    public String companylist(Model model){
        List<Company> companies=companyService.selectAll();
        model.addAttribute("companies",companies);
        return "companyindex";
    }

    @RequestMapping("/toaddcompany")
    public String toaddcompany(){
        return "addcompany";
    }

    @RequestMapping("/addcompany")
    public String addcompany(Company company){
        companyService.insert(company);
        return "redirect:/company/allcompany";
    }

    @RequestMapping("/toupdatacompany{id}")
    public String toupdata(@PathVariable("id") int id,Model model){
        Company findcompany=companyService.findcompanyByid(id);
        model.addAttribute("findcompany",findcompany);
        return "updatacompany";
    }

    @RequestMapping("/updatacompany")
    public String updata(Company company){
        companyService.updata(company);
        return "redirect:/company/allcompany";
    }

    @RequestMapping("/removecompany{id}")
    public String removecompany(@PathVariable("id") int id){
        companyService.delete(id);
        return "redirect:/company/allcompany";
    }

    @RequestMapping("/detailscompany{id}")
    public String detailcompany(@PathVariable("id") int id,Model model){
        List<Game> games=companyService.selectgamebyid(id);
        model.addAttribute("games",games);
        return "company_game";


    }


}
